<template>
    <view class="page">
        <shopCar v-if="!loading" :list="goodsProducts" @delect="delect" @accounts="accounts" />
        <u-modal v-model="show" show-cancel-button :content="content" @confirm="confirm"></u-modal>
        <TabBar :current="2"></TabBar>
    </view>
</template>
<script>
import { getCartList, planCart, delCart } from "@/api/mall.js"

export default {
    components: {

    },
    data() {
        return {
            mescroll: null, // mescroll实例对象 (此行可删,mixins已默认)
            // 下拉刷新的配置(可选, 绝大部分情况无需配置)
            downOption: {},
            // 上拉加载的配置(可选, 绝大部分情况无需配置)
            upOption: {
                use: false,
                toTop: {
                    src: "",
                },
            },
            isEdit: false,
            modalName: null,
            listTouchStart: 0,
            listTouchDirection: null,
            data: {},
            loading: true,
            //checked 1为未选择 2为选中
            goodsProducts: [
                // {
                //     name: '华为旗舰店',
                //     checked: 1,
                //     goods: [
                //         {
                //             img: '../../static/mall/goods_avatar.png',
                //             title: '华为荣耀',
                //             remark: '256G',
                //             price: '128.80',
                //             number: 1,
                //             checked: 1
                //         }, {
                //             img: '../../static/mall/goods_avatar.png',
                //             title: 'mate40',
                //             remark: '128G',
                //             price: '116.80',
                //             number: 1,
                //             checked: 1
                //         }
                //     ]
                // },
            ],
            show: false,
            content: '',
            delectData: []
        };
    },
    onReady() {

    },
    computed: {
    },
    onLoad() {
    },
    onShow() {
        this.judgeLogin(() => {
            this.getList()
        });
    },
    methods: {
        getList() {
            this.loading = true
            getCartList().then(res => {
                this.goodsProducts = []
                this.data = res.data ? res.data[0] : {}
                this.data.lists.forEach((e, i) => {
                    let newStr = e.goods_spec.replace(/::/g, ":")
                    e.specify = newStr.replace(/;;/g, ";")
                    e.checked = 1
                    this.goodsProducts.push({
                        goods: []
                    })
                    this.goodsProducts[i].goods.push(e)

                    console.log(this.goodsProducts, "this.goodsProducts")
                });
                this.loading = false
            }).catch(() => {
                this.loading = false
            })
        },
        delect(e) {
            this.delectData = e
            this.content = "是否要删除选中商品?"
            this.show = true
        },
        confirm() {
            delCart({ markings: this.getSelectStr(this.delectData) }).then((res) => {
                if (res.code == 1) {
                    this.$message("操作成功")
                    this.goodsProducts = []
                    this.getList()
                    this.show = false
                }
            })
        },
        accounts(e) {
            let array = [];
            e.forEach(element => {
                array.push(element.marking)
            });
            planCart({ markings: array.join(",") }).then((res) => {
                if (res.code == 1) {
                    let data = { ...res.data, ...{ type: 1 } }
                    uni.navigateTo({
                        url: '/pagesA/mall/orderconfirm?dataList=' + encodeURIComponent(JSON.stringify(data))
                    })
                } else {
                    this.$message(res.info)
                }
            })
        },
        getSelectStr(e) {
            let array = [];
            e.forEach(element => {
                array.push(element.marking)
            });
            return array.join(",")
        },
    },
};
</script>

<style scoped lang="scss">
.page {
  width: 750rpx;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: rgb(247, 247, 247);
}
/deep/.page-total {
  position: fixed;
  z-index: 999;
}
</style>
